﻿package mortal.game.control
{
    import Framework.MQ.*;
    import Message.BroadCast.*;
    import Message.Command.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.mainUI.zazen.*;
    import mortal.mvc.core.*;

    public class ZazenController extends Controller
    {
        private var _timer:SecTimer;
        private var _inviteZazenInfo:InviteZazenInfoTime;
        private var _zazenModule:ZazenModule;
        private var _isShowState:Boolean = false;
        private var _isNPCWindowShow:Boolean = false;

        public function ZazenController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePlayerInviteZazen, this.beIviteZazenHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePlayerZazenEarnings, this.zazenEarningHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePlayerReplayZazen, this.inviteZazenResult);
            NetDispatcher.addCmdListener(ECmdBroadCast._ECmdBroadcastEntityZazenUpdate, this.entityZazenUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ZazenGetEnergy, this.zazenEnergyUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ZazenGetExp, this.zazenExpUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ZazenGetLife, this.zazenLifeUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ZazenGetMana, this.zazenManaUpdate);
            Dispatcher.addEventListener(EventName.AskSingleZazen, this.singleZazenHandler);
            Dispatcher.addEventListener(EventName.ZazenProcessInviteInfo, this.processInviteInfoHandler);
            Dispatcher.addEventListener(EventName.NPCDialogWindowShow, this.onNPCDialogWindowShow);
            Dispatcher.addEventListener(EventName.NPCDialogWindowHide, this.onNPCDialogWindowHide);
            cache.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateStatus, this.roleStatusUpdateHandler);
            this._timer = new SecTimer();
            this._timer.addListener(TimerType.ENTERFRAME, this.onTimer);
            this._timer.start();
            return;
        }// end function

        private function onTimer(param1:SecTimer) : void
        {
            if (!Game.sceneInfo.limitInfo.canZazen())
            {
                return;
            }
            var _loc_2:* = GameManager.instance.lastOperTime;
            var _loc_3:* = getTimer();
            var _loc_4:* = Cache.instance.role.roleEntityInfo;
            if (GameMapUtil.curMapState.isSunbathMap)
            {
                if (_loc_2 > 0 && _loc_3 - _loc_2 > 5 * 1000)
                {
                    if (AIManager.isWorking || _loc_4.isInSun || RolePlayer.instance.isInSwinArea || _loc_4.isInPlayMusical || _loc_4.isInDance || _loc_4.isInCollect || _loc_4.isInToast)
                    {
                        GameManager.instance.updateLastOperTime();
                        return;
                    }
                    GameManager.instance.updateLastOperTime();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.SunbathCopyOper, {type:ESunbathOper._ESunbathOperBask, sEntityId:new SEntityId()}));
                }
            }
            else if (_loc_2 > 0 && _loc_3 - _loc_2 > 60 * 1000)
            {
                if (AIManager.isWorking || _loc_4.isTurned || _loc_4.fighting || _loc_4.isInZazen || _loc_4.isInCollect || RolePlayer.instance.isInSwinArea || _loc_4.isDead || GameController.scene.isInSky || _loc_4.entityInfo.level < GameConst.OpenZazenLevel)
                {
                    GameManager.instance.updateLastOperTime();
                    return;
                }
                GameManager.instance.updateLastOperTime();
                GameProxy.role.singleZazen();
            }
            return;
        }// end function

        private function entityZazenUpdate(param1:MessageBlock) : void
        {
            var _loc_5:String = null;
            var _loc_2:* = param1.messageBase as SEntityInfo;
            var _loc_3:* = _loc_2.entityId;
            var _loc_4:* = _loc_2.ownerEntityId;
            var _loc_6:* = cache.entity.getEntityInfoById(_loc_3);
            if (_loc_6)
            {
                _loc_5 = _loc_6.entityInfo.name;
                _loc_6.updateZazenEntityId(_loc_4);
            }
            _loc_6 = cache.entity.getEntityInfoById(_loc_4);
            if (_loc_6)
            {
                _loc_6.updateZazenEntityId(_loc_3);
            }
            return;
        }// end function

        private function zazenEnergyUpdate(param1:int) : void
        {
            if (ZazenIncomeView.hasInstance())
            {
                ZazenIncomeView.instace.addNimbus(param1);
            }
            return;
        }// end function

        private function zazenExpUpdate(param1:int) : void
        {
            if (ZazenIncomeView.hasInstance())
            {
                ZazenIncomeView.instace.addExp(param1);
            }
            return;
        }// end function

        private function zazenLifeUpdate(param1:int) : void
        {
            if (ZazenIncomeView.hasInstance())
            {
                ZazenIncomeView.instace.addLife(param1);
            }
            return;
        }// end function

        private function zazenManaUpdate(param1:int) : void
        {
            if (ZazenIncomeView.hasInstance())
            {
                ZazenIncomeView.instace.addMana(param1);
            }
            return;
        }// end function

        private function inviteZazenResult(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SReplayZazenInfo;
            var _loc_3:* = Language.getString(20441);
            if (_loc_2.result == true)
            {
                _loc_3 = Language.getString(20442);
            }
            MsgManager.showRollTipsMsg(_loc_2.playerInfo.name + _loc_3);
            return;
        }// end function

        private function beIviteZazenHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SInviteZazenInfo;
            if (this._zazenModule != null && this._zazenModule.isAutoReply)
            {
                this._inviteZazenInfo = new InviteZazenInfoTime(_loc_2);
                this.beIviteZazenClose(Alert.OK);
                return;
            }
            ZazenInviteicon.instance.addInfo(_loc_2);
            return;
        }// end function

        private function processInviteInfoHandler(event:DataEvent) : void
        {
            this._inviteZazenInfo = event.data as InviteZazenInfoTime;
            Alert.show(Language.getStringByParam(20443, this._inviteZazenInfo.inviteInfo.playerInfo.name), null, Alert.OK | Alert.CANCEL, null, this.beIviteZazenClose);
            return;
        }// end function

        private function beIviteZazenClose(param1:int) : void
        {
            var _loc_2:UserPlayer = null;
            if (param1 == Alert.OK)
            {
                _loc_2 = ThingUtil.entityUtil.getEntity(this._inviteZazenInfo.inviteInfo.playerInfo.entityId) as UserPlayer;
                if (_loc_2 == null)
                {
                    MsgManager.showRollTipsMsg(Language.getString(20444));
                    return;
                }
                if (_loc_2.entityInfo.isDoubleZazen)
                {
                    MsgManager.showRollTipsMsg(Language.getString(20445));
                    return;
                }
                if (!_loc_2.entityInfo.isSingleZazen)
                {
                    MsgManager.showRollTipsMsg(Language.getString(20444));
                    return;
                }
                if (!this._inviteZazenInfo.isInEffect())
                {
                    MsgManager.showRollTipsMsg(Language.getString(20446));
                    return;
                }
                AIManager.cancelAll();
                AIManager.addMoveTo(new Point(_loc_2.x2d, _loc_2.y2d), 10);
                AIManager.addCallback(this.zaZenOKWhenToTarget, this._inviteZazenInfo.inviteInfo.playerInfo.entityId);
                AIManager.start();
            }
            else
            {
                GameProxy.role.replyDoubleZazen(this._inviteZazenInfo.inviteInfo.playerInfo.entityId, false);
            }
            return;
        }// end function

        private function zaZenOKWhenToTarget(param1:SEntityId) : void
        {
            GameProxy.role.replyDoubleZazen(param1, true);
            return;
        }// end function

        private function onNPCDialogWindowShow(event:DataEvent) : void
        {
            this._isNPCWindowShow = true;
            this.updateZazenModuleVisible();
            return;
        }// end function

        private function onNPCDialogWindowHide(event:DataEvent) : void
        {
            this._isNPCWindowShow = false;
            this.updateZazenModuleVisible();
            return;
        }// end function

        private function updateZazenModuleVisible() : void
        {
            if (this._isShowState && !this._isNPCWindowShow)
            {
                if (this._zazenModule == null)
                {
                    this._zazenModule = UICompomentPool.getUICompoment(ZazenModule);
                }
                this._zazenModule.show();
            }
            else if (this._zazenModule)
            {
                this._zazenModule.hide();
            }
            return;
        }// end function

        private function roleStatusUpdateHandler(event:EntityInfoEvent) : void
        {
            var _loc_2:* = cache.role.roleEntityInfo;
            if (_loc_2.isSingleZazen)
            {
                MsgManager.showRollTipsMsg(Language.getString(20447));
                if (this._zazenModule == null)
                {
                    this._zazenModule = UICompomentPool.getUICompoment(ZazenModule);
                }
                this._isShowState = true;
                this.updateZazenModuleVisible();
            }
            else if (_loc_2.isDoubleZazen)
            {
                MsgManager.showRollTipsMsg(Language.getString(20448));
                if (this._zazenModule != null && this._zazenModule.isShowing)
                {
                    this._isShowState = false;
                    this.updateZazenModuleVisible();
                }
                if (ZazenInviteWin.hasInstance())
                {
                    ZazenInviteWin.instance.hide();
                }
            }
            else
            {
                if (this._zazenModule != null && this._zazenModule.isShowing)
                {
                    this._isShowState = false;
                    this.updateZazenModuleVisible();
                }
                if (ZazenInviteWin.hasInstance())
                {
                    ZazenInviteWin.instance.hide();
                }
            }
            return;
        }// end function

        private function singleZazenHandler(event:DataEvent) : void
        {
            if (!Game.sceneInfo.limitInfo.canZazen())
            {
                MsgManager.showRollTipsMsg(Language.getString(20449));
                return;
            }
            if (CollectAI.isWorking)
            {
                MsgManager.showRollTipsMsg(Language.getString(20450));
                return;
            }
            if (GameMapUtil.curMapState.isSunbathMap)
            {
                if (!cache.role.roleEntityInfo.isInSun)
                {
                    AIManager.cancelAll();
                    RolePlayer.instance.stopWalking();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.SunbathCopyOper, {type:ESunbathOper._ESunbathOperBask, sEntityId:new SEntityId()}));
                }
            }
            else if (!cache.role.roleEntityInfo.isInZazen)
            {
                AIManager.cancelAll();
                RolePlayer.instance.stopWalking();
                GameProxy.role.singleZazen();
            }
            return;
        }// end function

        private function zazenEarningHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SZazenEarnings;
            return;
        }// end function

    }
}
